package com.zimi.service;

import com.zimi.mapper.WfActivityLineMapper;
import com.zimi.mapper.WfActivityLineVOMapper;
import com.zimi.mapper.WfFormMapper;
import com.zimi.mapper.WfWorkflowMapper;
import com.zimi.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WfActivityLineService {

    @Autowired
    WfActivityLineMapper wfActivityLineMapper;
    @Autowired
    WfActivityLineVOMapper wfActivityLineVOMapper;
    @Autowired
    WfWorkflowMapper wfWorkflowMapper;
    @Autowired
    WfFormMapper wfFormMapper;


    public int deleteByPrimaryKey(Integer id) {
        return wfActivityLineMapper.deleteByPrimaryKey(id);
    }


    public int insert(WfActivityLine record) {
        return wfActivityLineMapper.insert(record);
    }


    public int insertSelective(WfActivityLine record) {
        return wfActivityLineMapper.insertSelective(record);
    }


    public WfActivityLine selectByPrimaryKey(Integer id) {
        return wfActivityLineMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(WfActivityLine record) {
        return wfActivityLineMapper.updateByPrimaryKeySelective(record);
    }


    public int updateByPrimaryKey(WfActivityLine record) {
        return wfActivityLineMapper.updateByPrimaryKey(record);
    }


    public List<WfActivityLine> selectAll(WfActivityLine record) {
        return wfActivityLineMapper.selectAll(record);
    }

    public void save_createLineFromStart(int wfId, WfActivity startActivity, WfActivity firstActivity) {
        WfActivityLine wfActivityLine = new WfActivityLine();
        wfActivityLine.setWfId(wfId);
        wfActivityLine.setSourceId(startActivity.getWfaId());
        wfActivityLine.setTargetId(firstActivity.getWfaId());
        wfActivityLine.setSourcePoint("BottomCenter");
        wfActivityLine.setTargetPoint("TopCenter");
        wfActivityLineMapper.insertSelective(wfActivityLine);
    }

    /*
     * @author: wj
     * @description: 根据ActivityIdc查询表单项目信息并返回
     * @date: 2019/4/3 13:54
     * @param
     * @return
     */
    public WfForm getWfForm(WfActivityLine wfActivityLine) throws Exception {
        WfForm returnWfForm = new WfForm();
        // 1.取得与流程绑定的表单情报
        WfWorkflow wfWorkflow = wfWorkflowMapper.findVOById(wfActivityLine.getWfId());
        if (wfWorkflow != null && wfWorkflow.getFormEditId() != null && wfWorkflow.getFormEditId() != 0) {
            WfForm wfForm = wfFormMapper.selectByPrimaryKey(wfWorkflow.getFormEditId());
            return wfForm;
        }
        return returnWfForm;
    }

    /*
     * @author: wj
     * @description: 保存活动连线
     * @date: 2019/4/3 15:19
     * @param
     * @return
     */
    public void saveActivityLine(WfActivityLine wfActivityLine) throws Exception {
        String activeFormula = wfActivityLine.getConditions();
        wfActivityLine.setConditions("");
        List<WfActivityLine> list = wfActivityLineMapper.selectAll(wfActivityLine);
        WfActivityLine newWfActivityLine = null;
        if (list != null && list.size() > 0) {
            newWfActivityLine = list.get(0);
            wfActivityLine.setConditions(activeFormula);
            wfActivityLine.setAlId(newWfActivityLine.getAlId());
        }
        if (wfActivityLine.getAlId() == null || wfActivityLine.getAlId() == 0) {
            wfActivityLineMapper.insertSelective(wfActivityLine);
        } else {
            wfActivityLineMapper.updateByPrimaryKeySelective(wfActivityLine);
        }
    }

    /*
     * @author: wj
     * @description: 删除活动连线（条件为流程id+sourceId+targetId）
     * @date: 2019/4/3 15:51
     * @param
     * @return
     */
    public void delLineBySourceTargetWfid(WfActivityLineVO wfActivityLineVO) throws Exception {
        wfActivityLineVOMapper.deleteLineBySourceTargetWfid(wfActivityLineVO);
    }

    public int deleteByWfId(WfActivityLineVO wfActivityLineVO) {
        return wfActivityLineVOMapper.deleteWfActivityLineByWfId(wfActivityLineVO);
    }
}
